package com.sdr.tackvideo;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.mylhyl.acp.Acp;
import com.mylhyl.acp.AcpListener;
import com.mylhyl.acp.AcpOptions;
import com.sdr.tackvideo.base.BaseActivity;
import com.sdr.tackvideo.tackvideo.TackVideoActivity;

import java.io.File;
import java.util.List;

public class MainActivity extends BaseActivity {

    private Toolbar mToolbar;
    private Activity mActivity;

    private static final int TACK_VIDEO_REQUEST_CODE = 0x000011;
    private static final int TACK_PHOTO_REQUEST_CODE = 0x000001;

    private TextView mTextView;

    private ImageView ivThum;

    private String PREVIEW_PATH = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mActivity = this;
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setStatusBar(mToolbar);

        mTextView = (TextView) findViewById(R.id.tvVideoPath);
        ivThum = (ImageView) findViewById(R.id.iv_thum);
        findViewById(R.id.btn_preVideo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(PREVIEW_PATH))return;
                Intent intent = new Intent(mActivity, TackVideoActivity.class);
                intent.putExtra(TackVideoActivity.TYPE, TackVideoActivity.PREVIEW_VIDEO);
                intent.putExtra(TackVideoActivity.PREVIEW_PATH, PREVIEW_PATH);
                startActivity(intent);
            }
        });

        findViewById(R.id.btn_startTack).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Acp.getInstance(MainActivity.this).request(new AcpOptions.Builder().setPermissions(
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.CAMERA).build(),
                        new AcpListener() {
                            @Override
                            public void onGranted() {
                                Intent intent = new Intent(mActivity, TackVideoActivity.class);
                                intent.putExtra(TackVideoActivity.TYPE, TackVideoActivity.TACK_VIDEO);
                                intent.putExtra(TackVideoActivity.SAVE_PATH, Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator+getPackageName());
                                startActivityForResult(intent,TACK_VIDEO_REQUEST_CODE);
                            }

                            @Override
                            public void onDenied(List<String> permissions) {
                                Toast.makeText(mActivity, "授权失败", Toast.LENGTH_SHORT).show();
                            }
                        });
            }
        });

        findViewById(R.id.btn_tackPhoto).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Acp.getInstance(MainActivity.this).request(new AcpOptions.Builder().setPermissions(
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE,
//                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.CAMERA).build(),
                        new AcpListener() {
                            @Override
                            public void onGranted() {

                                Intent intent = new Intent(mActivity, TackVideoActivity.class);
                                intent.putExtra(TackVideoActivity.TYPE, TackVideoActivity.TACK_PHOTO);
                                intent.putExtra(TackVideoActivity.SAVE_PATH, Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator+getPackageName());
                                startActivityForResult(intent,TACK_PHOTO_REQUEST_CODE);
                            }

                            @Override
                            public void onDenied(List<String> permissions) {

                            }
                        });
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == TACK_VIDEO_REQUEST_CODE && resultCode == RESULT_OK){
            String path = data.getStringExtra(TackVideoActivity.RESULT);
            Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Video.Thumbnails.MINI_KIND);
            bitmap = ThumbnailUtils.extractThumbnail(bitmap, 210, 210);
            ivThum.setImageBitmap(bitmap);
            PREVIEW_PATH = path;
            mTextView.setText("文件地址:"+PREVIEW_PATH);
            mTextView.append("\n文件大小:"+Formatter.formatFileSize(this,new File(path).length()));
        }
        if (requestCode == TACK_PHOTO_REQUEST_CODE && resultCode == RESULT_OK){
            String path = data.getStringExtra(TackVideoActivity.RESULT);
            ivThum.setImageBitmap(BitmapFactory.decodeFile(path));
            mTextView.setText("照片地址:"+path);
            mTextView.append("\n照片大小:"+Formatter.formatFileSize(this,new File(path).length()));
        }
    }
}
